<?php
/**
 * 首页API控制器
 * @author 王勇程 <laughskydragon@qq.com>
 */

namespace app\api\controller;

use app\api\controller\ApiBase;

/**
 * 首页控制器
 */
class Index extends ApiBase
{
    /**
     * 首页数据聚合
     */
    public function homeData()
    {
        $data = $this->request->param();
        return $this->logicIndex->homeData($data);
    }
    
    /**
     * 首页综合数据
     * 包含：轮播图、热门商品、市场概况、最新公告、推荐分类
     */
    public function homePage()
    {
        $data = $this->request->param();
        $result = $this->logicIndex->getHomePage($data);
        return $this->apiReturn($result);
    }

    /**
     * 首页快速入口数据
     * 包含：功能入口、快捷操作、用户状态
     */
    public function quickEntry()
    {
        $data = $this->request->param();
        $result = $this->logicIndex->getQuickEntry($data);
        return $this->apiReturn($result);
    }

    /**
     * 首页统计数据
     * 包含：市场总览、用户统计、热门数据
     */
    public function homeStats()
    {
        $data = $this->request->param();
        $result = $this->logicIndex->getHomeStats($data);
        return $this->apiReturn($result);
    }
    
    /**
     * 获取系统公告
     */
    public function announcements()
    {
        $data = $this->request->param();
        return $this->logicIndex->announcements($data);
    }
    
    /**
     * API文档首页（保留原有功能）
     */
    public function apiDoc()
    {
        $list = $this->logicDocument->getApiList([], true, 'sort');
        
        $code_list = $this->logicDocument->apiErrorCodeData();
        
        $this->assign('code_list', $code_list);
        
        $content = $this->fetch('content_default');

        $this->assign('content', $content);
        
        $this->assign('list', $list);
        
        return $this->fetch();
    }
    
    /**
     * API详情（保留原有功能）
     */
    public function details($id = 0)
    {
        $list = $this->logicDocument->getApiList([], true, 'sort');
        
        $info = $this->logicDocument->getApiInfo(['id' => $id]);
        
        $this->assign('info', $info);
        
        // 测试期间使用token ， 测试完成请删除
        $this->assign('test_access_token', get_access_token());
        
        $content = $this->fetch('content_template');
        
        if (IS_AJAX) {
            return throw_response_exception(['content' => $content]);
        }
        
        $this->assign('content', $content);
        
        $this->assign('list', $list);
        
        return $this->fetch('index');
    }
}
